package ua_gov_energy_vkek_icelineacct.mail;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author moroz
 */
public class Sender {

    Properties props;
    MailMeta mailMeta;

    public static class MailMeta {

        private String senderUserName, senderPassword,
                toAddresses, fromAddress, subj;

        public static final String MailMeta_PRE = "mail.meta" + ".";

        public MailMeta() {
        }

        public MailMeta(Properties props) {
            loadFromProps(props);
        }

        public final void loadFromProps(Properties props) {
            this.senderUserName = props.getProperty(MailMeta_PRE + "senderUserName");
            this.senderPassword = props.getProperty(MailMeta_PRE + "senderPassword");
            this.toAddresses = props.getProperty(MailMeta_PRE + "toAddresses");
            this.fromAddress = props.getProperty(MailMeta_PRE + "fromAddress");
            this.subj = props.getProperty(MailMeta_PRE + "subj");

        }
        // <editor-fold defaultstate="collapsed" desc="get&set methods">

        public String getSenderUserName() {
            return senderUserName;
        }

        public void setSenderUserName(String senderUserName) {
            this.senderUserName = senderUserName;
        }

        public String getSenderPassword() {
            return senderPassword;
        }

        public void setSenderPassword(String senderPassword) {
            this.senderPassword = senderPassword;
        }

        public String getToAddresses() {
            return toAddresses;
        }

        public void setToAddresses(String toAddresses) {
            this.toAddresses = toAddresses;
        }

        public String getFromAddress() {
            return fromAddress;
        }

        public void setFromAddress(String fromAddress) {
            this.fromAddress = fromAddress;
        }

        public String getSubj() {
            return subj;
        }

        public void setSubj(String subj) {
            this.subj = subj;
        }
        // </editor-fold>
    }

    public void send(String content) throws SendException {
        send(mailMeta, content);
    }

    public void send(MailMeta mm, String content) throws SendException {
        send(mm.getSenderUserName(), mm.getSenderPassword(),
                mm.getToAddresses(), mm.getFromAddress(), mm.getSubj(), content);
    }

    public void send(Properties mailProps, MailMeta mm, String content) throws SendException {
        setMailProps(mailProps);
        send(mm.getSenderUserName(), mm.getSenderPassword(),
                mm.getToAddresses(), mm.getFromAddress(), mm.getSubj(), content);
    }

    public void send(final String senderUserName, final String senderPassword,
            String toAddresses, String fromAdress, String subj, String content) throws SendException {

        try {
            Session sess = Session.getInstance(props,
                    new javax.mail.Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(senderUserName, senderPassword);
                        }
                    });

            sess.setDebug(true);
            sess.setDebugOut(System.out);
            // Transport transport = sess.getTransport("smtp");

            Message msg = new MimeMessage(sess);
            msg.setFrom(new InternetAddress(fromAdress));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddresses));
            msg.setText(content);
            msg.setSubject(subj);

            Transport.send(msg);

            //  transport.close();
        } catch (MessagingException ex) {
            throw new SendException("Send Error:"+ex.toString(), ex);
        }

    }

    // <editor-fold defaultstate="collapsed" desc="get&set methods">
    public void setMailProps(Properties mailProps) {
        this.props = mailProps;
    }

    public Properties getMailProps() {
        return props;
    }

    public Properties getProps() {
        return props;
    }

    public void setProps(Properties props) {
        this.props = props;
    }

    public MailMeta getMailMeta() {
        return mailMeta;
    }

    public void setMailMeta(MailMeta mailMeta) {
        this.mailMeta = mailMeta;
    }
    // </editor-fold>
}
